package game.leon.war.gate;

import game.leon.engine.net.GameSession;
import game.leon.engine.net.GateSession;
import game.leon.engine.net.codec.GameRawMessage;
import game.leon.war.defs.MessageConsts;
import game.leon.war.game.UserSessionService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * User: bianpeng
 * Date: 12-1-10
 * login自己处理掉, 其他的转发给game服务器
 */
@Component
public class GateHandlerService {
    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    private UserSessionService userSessionService;
    // 这个是节点的连接, 目前只做一个
    private GateSession nodeSession;
    public void handle(GateMessage message) {
        // 这个是node登陆到gate上来的消息
        if (message.getMessage().getMessage() < MessageConsts.MIN) {
            logger.info("engine message: " + message.getMessage().getMessage());
        } else if (message.getMessage().getMessage() == MessageConsts.LOGIN) {
            // TODO: 这里处理login, 获得userId, 并记录下来, 如果不成功, 则把连接断掉
            nodeSession = message.getSession();
        } else {
            // 转发给game服务器
            GameSession session = userSessionService.get(message.getMessage().getUserId());
            assert session != null;
            session.getIoSession().write(new GameRawMessage(
                    message.getMessage().getMessage(),
                    message.getMessage().getData()));
        }
    }
    public GateSession getNodeSession() {
        return nodeSession;
    }
}
